package ruleengine.db.mapper;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.util.List;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.extension.toolkit.ChainWrappers;
import com.ruleengine.db.mapper.RuleengineMapper;
import com.ruleengine.db.model.Ruleengine;

import ruleengine.BaseTest;

/**
 * @author lianghanwei on 2024/3/16
 */
public class RuleengineMapperTest extends BaseTest {

    @Autowired
    private RuleengineMapper ruleengineMapper;

    @Test
    public void selectList() {
        List<Ruleengine> list = ruleengineMapper.selectList(null);
        assertEquals(2, list.size());
        list.forEach(System.out::println);
    }

    @Test
    public void softDelete() {
        Ruleengine ruleengine = ChainWrappers.lambdaQueryChain(ruleengineMapper)
            .eq(Ruleengine::getTenantId, DEMO_TENANT_ID)
            .eq(Ruleengine::getInstanceId, DEMO_INSTANCE_ID)
            .one();
        assertNotNull(ruleengine);

        // UPDATE rule_engine SET status=1 WHERE status=0 AND (tenant_id = ? AND instance_id = ? AND ruleengine_id = ?)
        ChainWrappers.lambdaUpdateChain(ruleengineMapper)
            .eq(Ruleengine::getTenantId, DEMO_TENANT_ID)
            .eq(Ruleengine::getInstanceId, DEMO_INSTANCE_ID)
            .eq(Ruleengine::getRuleengineId, DEMO_RULEENGINE_ID)
            .remove();

        Ruleengine nullRuleengine = ChainWrappers.lambdaQueryChain(ruleengineMapper)
            .eq(Ruleengine::getTenantId, DEMO_TENANT_ID)
            .eq(Ruleengine::getInstanceId, DEMO_INSTANCE_ID)
            .one();
        assertNull(nullRuleengine);
    }
}
